package y2017.m10.d12;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UdpClient {

    public static void main(String[] args) throws Exception {
        //1.创建发送端的socket对象
        DatagramSocket ds = new DatagramSocket();

        InetAddress address = InetAddress.getByName("localhost");
        int port = 9999;

        //2.创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        String line;
        while ((line = sc.nextLine()) != null) {
            //键盘录入的数据line
            if ("q".equalsIgnoreCase(line))
                break;
            byte[] buf = line.getBytes();
            int length = buf.length;
            DatagramPacket dp = new DatagramPacket(buf, length, address, port);
            //发送数据包
            ds.send(dp);
            DatagramPacket get = new DatagramPacket(buf, length, address, port);
            ds.receive(get);
            System.out.println(new String(buf));
        }

        //释放资源
        ds.close();
    }
}
